package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @描述：前台页面控制器
 */
@Controller
@RequestMapping("/hotel")
public class WebController {

    @Autowired
    IUserService userService;

    @Autowired
    private IAboutService aboutService;
    @Autowired
    private INavService navService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IBookService bookService;

    //关于我们页面
    @RequestMapping(value = "/about")
    public String about(Model model) {
        List<About> aboutList = aboutService.selectAll();
        model.addAttribute("aboutList", aboutList);

        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);
        return "about";
    }

//    首页
    @RequestMapping(value = "/index")
    public String index(Model model) {
        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

        List<About> aboutList = aboutService.selectAll();
        model.addAttribute("aboutList", aboutList);

        return "index";

    }

    //客服概况页面
    @RequestMapping(value = "/rooms")
    public String rooms(Model model) {
        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

        List<Room> roomList = roomService.selectByStatus();
        model.addAttribute("roomList", roomList);


        return "rooms";
    }

    //    房间详情
    @RequestMapping(value = "/detail")
    public String detail(Model model, Integer id) {
        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

        if (id != null) {
            Room room = roomService.selectById(id);
            model.addAttribute("room", room);
        }
        return "detail";
    }

    //    预订房间页面
    @RequestMapping(value = "/user/book")
    public String book(Model model, Integer id) {
        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

        if (id != null) {
            Room room = roomService.selectById(id);
            model.addAttribute("room", room);
        }
        return "/user/book";
    }

    //    预订成功界面
    @RequestMapping(value = "/user/book_ok")
    public String book_ok(Model model, Book book, HttpServletRequest request) {
        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

        HttpSession session = request.getSession();
        String loginName = (String)session.getAttribute("loginName");
        book.setUserName(loginName);
        Date date = new Date();
        book.setBookdate(date);

        //根据book的实体类把那些name对应的属性填进去

        bookService.insert(book);
        return "/user/book_ok";
    }

    @RequestMapping("/user/toLogin")
    public String toLogin() {
        return "userLogin";
    }

    @RequestMapping("/user/toReg")
    public String toReg() {
        return "register";
    }

    @RequestMapping("/user/doLogin")
    public String doLogin(String username, String password, Model model, HttpSession session) {
        User loginUser = userService.doLogin(username);

        if (loginUser != null && loginUser.getPassword().equals(password)) {
            model.addAttribute("loginUserName", loginUser.getUsername());
            //添加到session中便于拦截器放行
            session.setAttribute("loginName", loginUser.getUsername());
            return "redirect:/hotel/index";
        } else {
            model.addAttribute("loginTip", "用户名或密码错误，请重试");
            return "/userLogin";
        }
    }

    @RequestMapping("/user/doReg")
    public String doReg(User user, Model model) {
        //查询用户名是否重复
        User regUser = userService.checkName(user.getUsername());
        //如果查询的user为null，就说明没有重复的名字，则在数据库中添加注册用户的信息
        if (regUser == null) {
            //添加用户信息
            userService.addUser(user);
            System.out.println("用户添加完成...");
            return "userLogin";
        } else {
            //如果用户名重复，就添加提示信息
            model.addAttribute("regTip", "用户名已存在！请重试");
            return "register";
        }

    }

    @RequestMapping("/user/toMine")
    public String toMine(String username, Model model, HttpServletRequest request) {

        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

        HttpSession session = request.getSession();
        String loginName = (String) session.getAttribute("loginName");
        List<Book> mybookList = bookService.selectByUserName(loginName);
        model.addAttribute("mybookList",mybookList);
        return "user/mine";
    }

    //   查看我的订单
    @RequestMapping(value = "/user/mine")
    public String mine(Model model, Integer id, HttpSession session) {
        Object user = session.getAttribute("loginName");
        String username = (String) user;
        //导航
        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

//        获取自己预约的房间信息
        List<Book> mybookList = bookService.selectByUserName(username);
        model.addAttribute("mybookList", mybookList);
        if (id != null) {
            Room room = roomService.selectById(id);
            model.addAttribute("room", room);
        }
        return "/user/mine";
    }

    @RequestMapping("/user/toMineDetail")
    public String toMineDetail(String username, Model model, HttpServletRequest request, Integer id) {

        List<Nav> navList = navService.getAll();
        model.addAttribute("navList", navList);

        HttpSession session = request.getSession();
        String loginName = (String) session.getAttribute("loginName");
        Book book = bookService.selectById(id);
        model.addAttribute("book",book);
        return "user/mineDetail";
    }

    @RequestMapping("/user/deleteOrder")
    public String delete(Integer id) {

        bookService.delete(id);
        return "redirect:/hotel/user/mine";
    }

    @RequestMapping("/logout")
    public String Logout(HttpSession session) {

        session.removeAttribute("loginName");

        return "redirect:/hotel/index";
    }

}
